﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

namespace FYPHP
{
    public partial class SearchMember2 : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);
       
        protected void Page_Load(object sender, EventArgs e)
        {

            try
            {
                MultiView1.ActiveViewIndex = 0;

                SqlCommand cmdUser;
                SqlDataReader dtrUser;
                conn.Open();

                cmdUser = new SqlCommand("Select * From Users where u_id = @Id", conn);
                cmdUser.Parameters.AddWithValue("@Id",Request.QueryString["id"]);
                dtrUser = cmdUser.ExecuteReader();
                dtrUser.Read();

          

                String userName = dtrUser["username"].ToString();
                String gender = dtrUser["u_gender"].ToString();
                String age = dtrUser["u_age"].ToString();
                string addressBeforeSplit = dtrUser["u_address"].ToString();

                string[] lines = addressBeforeSplit.Split('|');

                string address = "";

                foreach (string line in lines)
                {
                    address += " " + line;
                }
                String email = dtrUser["u_email"].ToString();
                String rewardpoint = dtrUser["u_points"].ToString();
                String UserStatus = dtrUser["u_status"].ToString();

                lblPID.Text = Request.QueryString["id"].ToString();
                lblName.Text = userName;
                lblGender.Text = gender;
                lblAge.Text = age;
                lblAddress.Text = address;
                lblEmail.Text = email;
                lblRewardPoints.Text = rewardpoint;
               
                dtrUser.Close();
                conn.Close();

                if (UserStatus.Equals("Activate"))
                {
                    btnMemberActivation.Text = "Deactivate My Account1";
                }
                else if (UserStatus.Equals("Deactivate"))
                {
                    btnMemberActivation.Text = "Activate My Account1";

                }

            }
            catch (SqlException ex)
            {

            }



        }

  

        public String return_userpicture()
        {

            String userPicture;
            SqlCommand cmdUser;
            SqlDataReader dtrUser;
            conn.Open();

            cmdUser = new SqlCommand("Select * From Users where u_id =@Id", conn);

            cmdUser.Parameters.AddWithValue("@Id",Request.QueryString["id"]);
            dtrUser = cmdUser.ExecuteReader();

            dtrUser.Read();

            userPicture = dtrUser["u_picture"].ToString();

            dtrUser.Close();
            conn.Close();


            return userPicture;

        }

        protected void btnEditProfile_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminEditMemberProfile.aspx?id=" + Request.QueryString["id"].ToString());
        }

        protected void btnMemberActivation_Click(object sender, EventArgs e)
        {

            try
            {
                SqlCommand cmdUser;
                SqlDataReader dtrUser;
                conn.Open();

           

                cmdUser = new SqlCommand("Select * From Users where u_id=@UID ", conn);
                cmdUser.Parameters.AddWithValue("@UID", Request.QueryString["id"]);


                dtrUser = cmdUser.ExecuteReader();
                dtrUser.Read();

                String UserStatus = dtrUser["u_status"].ToString();

                dtrUser.Close();

                if (UserStatus.Equals("Activate"))
                {

                    SqlCommand cmdUpdate = new SqlCommand("Update Users SET u_status=@STATUS where u_id =@UID ", conn);
                    cmdUpdate.Parameters.AddWithValue("@STATUS", "Deactivate");
                    cmdUpdate.Parameters.AddWithValue("@UID",Request.QueryString["id"]);
                    cmdUpdate.ExecuteNonQuery();
                    btnMemberActivation.Text = "Activate My Account2";
                }
                else if (UserStatus.Equals("Deactivate"))
                {
                    SqlCommand cmdUpdate = new SqlCommand("Update Users SET u_status=@STATUS where u_id =@UID ", conn);
                    cmdUpdate.Parameters.AddWithValue("@STATUS", "Activate");
                    cmdUpdate.Parameters.AddWithValue("@UID",Request.QueryString["id"]);
                    cmdUpdate.ExecuteNonQuery();
                    btnMemberActivation.Text = "Deactivate My Account2";
                }


                conn.Close();



            }
            catch (SqlException ex)
            {


            }
        }

   

    }
}